//## begin module%3B8825A703A8.cm preserve=no
//	  %X% %Q% %Z% %W%
//## end module%3B8825A703A8.cm

//## begin module%3B8825A703A8.cp preserve=no
//	+----------------------------------------------+
//	| SVMBR - A program for training SVMs          |
//	+----------------------------------------------+
//	| * Creator, modeling, coding:                 |
//	|    Marcelo Barros de Almeida                 |
//	|    barros@smar.com.br                        |
//	|    http://litc.cpdee.ufmg.br/~barros/        |
//	| * Coding, improvements, bug fixes:           |
//	|    Bernardo Penna                            |
//	|    bprc@brfree.com.br                        |
//	+----------------------------------------------+
//
//	 Copyright(c) 2002 by Marcelo Barros de Almeida
//	                            All rights reserved
//
//	-[CVS]------------------------------------------
//	$Author$
//	$Date$
//	$Source$
//	$Name$
//	$Revision$
//	------------------------------------------------
//## end module%3B8825A703A8.cp

//## Module: solver%3B8825A703A8; Package body
//## Subsystem: <Top Level>
//## Source file: D:\textotese\svmbr\src\solver.cpp

//## begin module%3B8825A703A8.additionalIncludes preserve=no
//## end module%3B8825A703A8.additionalIncludes

//## begin module%3B8825A703A8.includes preserve=yes
#include "pqueue.h"
//## end module%3B8825A703A8.includes

// svm
#include "svm.h"
// solver
#include "solver.h"


//## begin module%3B8825A703A8.declarations preserve=no
//## end module%3B8825A703A8.declarations

//## begin module%3B8825A703A8.additionalDeclarations preserve=yes
//## end module%3B8825A703A8.additionalDeclarations


// Class Solver 







//## begin Solver::EDR%3B882D530009.attr preserve=no  public: static SHORT {U} 1
SHORT Solver::EDR = 1;
//## end Solver::EDR%3B882D530009.attr

//## begin Solver::SMO%3B882D5F03C8.attr preserve=no  public: static SHORT {U} 2
SHORT Solver::SMO = 2;
//## end Solver::SMO%3B882D5F03C8.attr

//## begin Solver::BOOSTING%3BA9FF510196.attr preserve=no  public: static SHORT {UA} 3
SHORT Solver::BOOSTING = 3;
//## end Solver::BOOSTING%3BA9FF510196.attr














//## Other Operations (implementation)
REAL Solver::evalOutput (UINT i1)
{
  //## begin Solver::evalOutput%998688348.body preserve=yes
	cerr << "(Solver::evalOutput) Not implemented" << endl ;
	return 0;
  //## end Solver::evalOutput%998688348.body
}

REAL Solver::evalKernel (UINT i1, UINT i2)
{
  //## begin Solver::evalKernel%998688351.body preserve=yes

	return cache->evalKernel(i1,i2);	

  //## end Solver::evalKernel%998688351.body
}

INT Solver::saveResult (CHAR *file)
{
  //## begin Solver::saveResult%998688352.body preserve=yes
	cerr << "(Solver::saveResult) Not implemented" << endl ;
	return FALSE;

  //## end Solver::saveResult%998688352.body
}

VOID Solver::calcBias ()
{
  //## begin Solver::calcBias%999084993.body preserve=yes
	INT i,j;
	REAL baver = 0;
	INT dataSize = data->getnumExamples();
  for(i = 0, j = 0; i < dataSize; i++){
		if((alpha[i]>epsilon)&&(alpha[i]<(C-epsilon))){
			baver += -errorCache[i];
			j++;
		}
	}
	if(j == 0){
		cerr << "Could not determine bias:  unbounded SV not found" ;
	}
	else
		bias = baver/j;
  //## end Solver::calcBias%999084993.body
}

REAL Solver::getElapsedTime ()
{
  //## begin Solver::getElapsedTime%999139393.body preserve=yes
	return crono.getElapsedTime();
  //## end Solver::getElapsedTime%999139393.body
}

VOID Solver::init (REAL cv, REAL tolv, REAL epsilonv, SVM *svmp, Kernel *kernelp, Data *datap, INT chunksizev)
{
  //## begin Solver::init%1000125175.body preserve=yes
        C       = cv;
        tol     = tolv;
        epsilon = epsilonv;
        svm     = svmp;
        kernel  = kernelp;
        data    = datap;
        
        bias    = 0;
        oldBiasSubset = 0;
        numIter = 0;
	
	INT size = data->getnumExamples();

	if((chunksizev >= size)||(chunksizev <= 0))
		chunkSize = size;
	else
		chunkSize = chunksizev;

	try{
		oldAlphaSubset   = new REAL [chunkSize];
		subset     = new INT  [chunkSize];
		supset     = new INT  [size - chunkSize];
		alpha      = new REAL [size];
		errorCache = new REAL [size];
		cache      = new Cache(kernel,data,chunkSize);

		if(chunkSize == size){
			cache->DontUseChunk();
			for(INT i = 0 ; i < chunkSize ; i++)
				subset[i] = i;
		}

		for(INT i = 0 ; i < size ; i++){
			alpha[i]      = 0;
			oldAlphaSubset[i] = 0;
			errorCache[i] = -1*data->gettarget(i);
		}	
	} 
	catch(...){
		cerr << "Error when allocating memory (Solver::init)." << endl;
		exit(1);
	}
  //## end Solver::init%1000125175.body
}

VOID Solver::freeSpace ()
{
  //## begin Solver::freeSpace%1000125176.body preserve=yes
	delete alpha;
	delete errorCache;
	delete cache;
	delete oldAlphaSubset;
	delete subset;
	delete supset;
  //## end Solver::freeSpace%1000125176.body
}

// Additional Declarations
  //## begin Solver%3B87FD370399.declarations preserve=yes
  //## end Solver%3B87FD370399.declarations

//## begin module%3B8825A703A8.epilog preserve=yes
//## end module%3B8825A703A8.epilog
